package com.gxwgy.wg2404.controller;

import com.gxwgy.wg2404.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;

/**
 * @author 姚相国
 * @version 1.0
 * @className OrganizerEditServlet 此类是修改数据的
 * @date 2024/12/18 03:14
 */
@WebServlet({"/organizer/editEvent2"})
public class OrganizerEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //判断是否获取到了session
        HttpSession session = request.getSession(false);
        // 如果有session，就表示已登录，可以执行相关操作
        if (session.getAttribute("username") == null) {
            request.getRequestDispatcher("/index.jsp").forward(request,response);
        }

        String role = (String) session.getAttribute("role");


        // 获取表单数据
        int eventId = Integer.parseInt(request.getParameter("eventId"));
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String startTime = request.getParameter("start_time");
        String endTime = request.getParameter("end_time");
        String location = request.getParameter("location");
        int capacity = Integer.parseInt(request.getParameter("capacity"));

        Connection conn = null;
        PreparedStatement ps = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "UPDATE events SET name = ?, description = ?, start_time = ?, end_time = ?, location = ?, capacity = ?, updated_at = CURRENT_TIMESTAMP WHERE id = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, name);
            ps.setString(2, description);
            ps.setString(3, startTime);
            ps.setString(4, endTime);
            ps.setString(5, location);
            ps.setInt(6, capacity);
            ps.setInt(7, eventId);
            int rowsAffected = ps.executeUpdate();

            if (rowsAffected > 0) {
                // 根据角色或其他条件执行不同的重定向
                if ("organizer".equals(role)) {
                    response.setContentType("text/html; charset=UTF-8");
                    response.getWriter().println("<script>alert('修改成功！返回活动列表页面'); location.href='" + request.getContextPath() + "/organizer/dashboard';</script>");
                } else if ("admin".equals(role)) {
                    response.setContentType("text/html; charset=UTF-8");
                    response.getWriter().println("<script>alert('修改成功！返回管理面板'); location.href='" + request.getContextPath() + "/admin/dashboard';</script>");
                } else {
                    // 默认的重定向逻辑，或者处理其他角色
                    response.setContentType("text/html; charset=UTF-8");
                    response.getWriter().println("<script>alert('删除成功！'); location.href='" + request.getContextPath() + "/default/dashboard';</script>");
                }
            } else {
                response.sendRedirect(request.getContextPath() + "/error.jsp");
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
    }
}
